package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.PasswordConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.EmployeeDTO;
import com.sky.dto.EmployeeLoginDTO;
import com.sky.dto.EmployeePageQueryDTO;
import com.sky.dto.PasswordEditDTO;
import com.sky.entity.Employee;
import com.sky.exception.AccountLockedException;
import com.sky.exception.AccountNotFoundException;
import com.sky.exception.PasswordEditFailedException;
import com.sky.exception.PasswordErrorException;
import com.sky.mapper.EmployeeMapper;
import com.sky.result.PageResult;
import com.sky.service.EmployeeService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.nio.charset.StandardCharsets;
import java.util.List;

@Service
public class EmployeeServiceImpl implements EmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;

    /**
     * 员工登录
     *
     * @param employeeLoginDTO
     * @return
     */
    public Employee login(EmployeeLoginDTO employeeLoginDTO) {
        String username = employeeLoginDTO.getUsername();
        String password = employeeLoginDTO.getPassword();

        //1、根据用户名查询数据库中的数据
        Employee employee = employeeMapper.getByUsername(username);

        //2、处理各种异常情况（用户名不存在、密码不对、账号被锁定）
        if (employee == null) {
            //账号不存在
            throw new AccountNotFoundException(MessageConstant.ACCOUNT_NOT_FOUND);
        }

        //密码比对
        // TODO 后期需要进行md5加密，然后再进行比对
        if (!password.equals(employee.getPassword())) {
            //密码错误
            throw new PasswordErrorException(MessageConstant.PASSWORD_ERROR);
        }

        if (employee.getStatus() == StatusConstant.DISABLE) {
            //账号被锁定
            throw new AccountLockedException(MessageConstant.ACCOUNT_LOCKED);
        }

        //3、返回实体对象
        return employee;
    }

    /**
     * @Author Lemon
     * @Description 分页查询员工信息
     * @Date 2025-01-07 11:46
     * @Param [employeePageQueryDTO]
     * @Return com.sky.result.PageResult
     */
    @Override
    public PageResult pageEmployee(EmployeePageQueryDTO employeePageQueryDTO) {
        //设置分页参数
        PageHelper.startPage(employeePageQueryDTO.getPage(),employeePageQueryDTO.getPageSize());
        //根据条件查询数据
        List<Employee> employeeList = employeeMapper.pageEmployee(employeePageQueryDTO.getName());
        //将查询结果进行分页
        Page<Employee> employeePage = (Page<Employee>) employeeList;
        //封装返回结果
        PageResult pageResult = new PageResult();
        pageResult.setTotal(employeePage.getTotal());
        pageResult.setRecords(employeePage.getResult());
        return pageResult;
    }

    /**
     * @Author Lemon
     * @Description 新增员工
     * @Date 2025-01-07 14:59
     * @Param [employeeDTO]
     * @Return void
     */
    @Override
    public void addEmployee(EmployeeDTO employeeDTO) {
        Employee employee = new Employee();
        BeanUtils.copyProperties(employeeDTO, employee);
        //将初始密码123456用MD5加密
        String md5Pass = DigestUtils.md5DigestAsHex(PasswordConstant.DEFAULT_PASSWORD.getBytes(StandardCharsets.UTF_8));
        //设置密码
        employee.setPassword(md5Pass);
        //设置默认状态
        employee.setStatus(StatusConstant.ENABLE);
        employeeMapper.addEmployee(employee);
    }

    /**
     * @Author Lemon
     * @Description 根据id查询员工信息
     * @Date 2025-01-07 16:06
     * @Param [id]
     * @Return com.sky.entity.Employee
     */
    @Override
    public Employee getEmployeeById(Long id) {
        Employee employee = employeeMapper.getEmployeeById(id);
        return employee;
    }

    /**
     * @Author Lemon
     * @Description 根据id启用、禁用员工账号
     * @Date 2025-01-07 17:29
     * @Param [status, id]
     * @Return void
     */
    @Override
    public void changeStatus(String status, String id) {
        employeeMapper.changeStatus(status,id);
    }

    /**
     * @Author Lemon
     * @Description 修改密码
     * @Date 2025-01-14 10:01
     * @Param [passwordEditDTO]
     * @Return void
     */
    @Override
    public void editPassword(PasswordEditDTO passwordEditDTO) {

        //判断账号是否存在
        if (StringUtils.isEmpty(passwordEditDTO.getEmpId())){
            throw new PasswordEditFailedException(MessageConstant.ACCOUNT_NOT_FOUND);
        }

        Employee employee = employeeMapper.getEmployeeById(passwordEditDTO.getEmpId());
        if (ObjectUtils.isEmpty(employee)){
            throw new PasswordEditFailedException(MessageConstant.ACCOUNT_NOT_FOUND);
        }

        //判断原密码是否正确
        if (employee.getPassword().equals(passwordEditDTO.getOldPassword())){
            throw new PasswordEditFailedException(MessageConstant.PASSWORD_ERROR);
        }

        //判断新密码是否与旧密码相同
        if (passwordEditDTO.getOldPassword().equals(passwordEditDTO.getNewPassword())) {
            throw new PasswordEditFailedException(MessageConstant.PASSWORD_NOT_OLDEQUALNEW);
        }

        //将新密码用MD5加密
        String md5Pass = DigestUtils.md5DigestAsHex(passwordEditDTO.getNewPassword().getBytes(StandardCharsets.UTF_8));
        passwordEditDTO.setNewPassword(md5Pass);
        employeeMapper.editPassword(passwordEditDTO);

    }

    /**
     * @Author Lemon
     * @Description 编辑员工信息
     * @Date 2025-01-14 14:47
     * @Param [employeeDTO]
     * @Return void
     */
    @Override
    public void updateEmployee(EmployeeDTO employeeDTO) {
        Employee employee = new Employee();
        BeanUtils.copyProperties(employeeDTO, employee);

        employeeMapper.updateEmployee(employee);

    }

}
